![]() |
PATH![]() |
![]() ![]() |
The Copy and Set commands both assign values to variables, but they produce different results when the value assigned is a script object. The Copy command makes a new copy of the script object, while the Set command creates a variable that shares data with the original script object. Note that this behavior (Copy creates a new copy, Set shares the original data) is the same when you work with lists and records, as described in Data Sharing .
To examine how Copy and Set work with script objects, consider the following example, which defines a script object, called John , with a property called Vegetable.
script John
property Vegetable: "Spinach"
end script
set myScriptObject to John
set Vegetable of John to "Swiss chard"
get Vegetable of myScriptObject
--result: "Swiss chard"
The first Set command defines a variable, called myScriptObject , that shares data with the original script object John . The second Set command changes the value of the Vegetable property of script object John from "Spinach" to "Swiss chard" . Because myScriptObject shares data with John , it shares the change to the Vegetable property of John . When you get the Vegetable property of myScriptObject , the result is "Swiss chard" .
Now consider the following example, which uses the Copy command to define the variable myScriptObject .
script John
property Vegetable: "Spinach"
end script
copy John to myScriptObject
set Vegetable of John to "Swiss chard"
get Vegetable of myScriptObject
--result: "Spinach"
In this case, the Copy command creates a new script object. Setting the Vegetable property of the original script object has no effect on the new script object. The result of the Get command is "Spinach" .
When you copy a child script object to a variable, the variable contains a complete copy of both the child and its parent, including all the parent's properties and handlers. Each new copy, including its inherited properties and handlers, is completely independent of both the original and any other copies.
For example, if you copy a modified version of the JohnSon script in this example to two different variables, you can set each variable's Vegetable property independently:
script John
property Vegetable : "Spinach"
end script
script JohnSon
property parent : John
on changeVegetable(x)
set my Vegetable to x
end changeVegetable
end script
copy JohnSon to J1
copy JohnSon to J2
tell J1 to changeVegetable("Zucchini")
tell J2 to changeVegetable("Swiss chard")
Vegetable of J1
--result: "Zucchini"
Vegetable of J2
--result: "Swiss chard"
Vegetable of John
--result: "Spinach"
You can create handlers that construct copies of script objects for use elsewhere in a script. For example, the script that follows includes a handler that takes an initial balance as a parameter and creates a copy of a script object that acts as an independent account. Each copy includes several properties and an on deposit handler that enables the script object to increment its own balance when it receives a Deposit command.
on makeAccount(initialBalance)
script account
property StartDate : current date
property Balance : initialBalance
on deposit(amount)
set Balance to Balance + amount
end deposit
end script
end makeaccount
set a to makeAccount(3300)
set b to makeAccount(33)
tell a
deposit(30)
deposit(60)
end tell
{Balance of a, StartDate of a}
--result: {3390, date "Saturday, February 13, 1999 12:12:57 PM"}
{Balance of b, StartDate of b}
--result: {33, date "Saturday, February 13, 1999 12:13:38 PM"}